/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thesis.haipv.qa.persistence;

import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import thesis.haipv.qa.comtrust.ComTrust;
import thesis.haipv.qa.model.Question;
import thesis.haipv.qa.model.UserProfile;

/**
 *
 * @author PHANHAI
 */
public class UserProfileDAO {

    private DatabaseContext db;
    
    public UserProfileDAO() {
        db = DatabaseContext.getInstance();
    }
    
    /**
     * Create new user profile.
     * @param profile 
     */
    public void createUserProfile(UserProfile profile) {
        if(checkExist(profile.getAccountId())) {
            return;
        }
        String str = "INSERT INTO userprofiles " 
                + "(AccountId, Username, Fullname, Password, Createtime) VALUES (?, ?, ?, ?, ?)";
        try {
            PreparedStatement ps = (PreparedStatement) db.getConnection().prepareStatement(str);
            ps.setString(1, profile.getAccountId());
            ps.setString(2, profile.getUsername());
            ps.setString(3, profile.getFullname());
            ps.setString(4, profile.getPassword());
            ps.setLong(5, profile.getCreateTime());
            ps.executeUpdate();
            ps.close();
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
    } 
    
    public UserProfile login(String username, String pwd) {
        UserProfile up = null;
        String query = "SELECT AccountId, Username, Password, Fullname, CreateTime FROM userprofiles "
                + "WHERE Username = ? AND Password = ? LIMIT 1";
        try {
            PreparedStatement ps = (PreparedStatement) db.getConnection().prepareStatement(query);
            ps.setString(1, username);
            ps.setString(2, pwd);
            ResultSet rs = ps.executeQuery();
            if(rs.next()) {
                up = new UserProfile(rs.getString("AccountId"), rs.getString("fullname"));
            }
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return up;
    }
    
    public UserProfile getProfile(String username) {
        UserProfile up = null;
        String query = "SELECT AccountId, Fullname FROM userprofiles "
                + "WHERE AccountId = ? LIMIT 1";
        try {
            PreparedStatement ps = (PreparedStatement) db.getConnection().prepareStatement(query);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()) {
                up = new UserProfile(rs.getString("AccountId"), rs.getString("Fullname"));
                up.setExperienceTrust(getReferienceTrust(username));
            }
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return up;
    }
    
    public boolean checkExist(String accountId) {
        String query = "SELECT * FROM userprofiles "
                + "WHERE AccountId = ? LIMIT 1";
        try {
            PreparedStatement ps = (PreparedStatement) db.getConnection().prepareStatement(query);
            ps.setString(1, accountId);

            ResultSet rs = ps.executeQuery();
            if(rs.next()) {
                return true;
            }
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public float getReferienceTrust(String userId) {
        ExperienceTrustDAO edao = new ExperienceTrustDAO();
        return ComTrust.getReferienceTrust(edao.getReferienceTrust(userId));
    }
    
    public float getExperienceTrustWith(String userId, String partnerId, long time) {
        ExperienceTrustDAO edao = new ExperienceTrustDAO();
        return edao.getExperienceTrust(userId, partnerId);
    }
    
}
